package com.manor.paradise.fragment

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.manor.common.http.JHttpCallback
import com.manor.common.utils.GsonUtil
import com.manor.common.utils.KLog
import com.manor.common.utils.ToastUtil
import com.manor.paradise.R
import com.manor.paradise.adapter.GiveInfoAdapter
import com.manor.paradise.bean.GiceInfoBean
import com.manor.paradise.http.ElvesHttpConst
import com.manor.paradise.http.ElvesHttpUtil
import kotlinx.android.synthetic.main.fragment_gice_info.*

class GiveInfoFragment(context: Context,var sun :Int) : Fragment(){
    var mCurView: View? = null
    internal var pageNum = 1
    internal lateinit var mAdapter: GiveInfoAdapter
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        mCurView = inflater.inflate(R.layout.fragment_gice_info, container, false)
        return mCurView
    }
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        rcv_list.layoutManager = LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false)
        mAdapter = GiveInfoAdapter(R.layout.item_gice_info,null,sun)
        rcv_list.setAdapter(mAdapter)

        refreshLayout.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onLoadMore(refreshLayout: RefreshLayout) {
                pageNum++
                setData()
            }

            override fun onRefresh(refreshLayout: RefreshLayout) {
                pageNum = 1
                setData()
            }
        })
    }
    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        super.setUserVisibleHint(isVisibleToUser)
        if(isVisibleToUser){
            setData()
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }
    fun setData() {
        if(sun==0){
            ElvesHttpUtil.getExpend(pageNum, object : JHttpCallback() {
                override fun onSuccess(code: Int, msg: String, info: String) {
                    if (code != 200) {
                        ToastUtil.show(msg)
                        return
                    }
                    var shopListBean: GiceInfoBean = GsonUtil.gsonToBean<GiceInfoBean>(info, GiceInfoBean::class.java)
                    KLog.e("json", shopListBean);
                    if(pageNum==1){
                        mAdapter.setNewInstance(shopListBean.records)
                        refreshLayout?.finishRefresh()
                        if(shopListBean.records.size==0){
                            mEmptyView?.showNull()
                        }else{
                            mEmptyView?.visibility = View.GONE
                        }
                    }else{
                        mAdapter.addData(shopListBean.records)
                        refreshLayout?.finishLoadMore()
                    }

                }

                override fun onError() {
                    super.onError()

                }
            })
        }else{
            ElvesHttpUtil.getIncome(pageNum, object : JHttpCallback() {
                override fun onSuccess(code: Int, msg: String, info: String) {
                    if (code != 200) {
                        ToastUtil.show(msg)
                        return
                    }
                    var shopListBean: GiceInfoBean = GsonUtil.gsonToBean<GiceInfoBean>(info, GiceInfoBean::class.java)
                    KLog.e("json", shopListBean);
                    if(pageNum==1){
                        mAdapter.setNewInstance(shopListBean.records)
                        refreshLayout?.finishRefresh()
                        if(shopListBean.records.size==0){
                            mEmptyView?.showNull()
                        }else{
                            mEmptyView?.visibility = View.GONE
                        }
                    }else{
                        mAdapter.addData(shopListBean.records)
                        refreshLayout?.finishLoadMore()
                    }

                }

                override fun onError() {
                    super.onError()

                }
            })
        }

    }
    override fun onDestroy() {
        ElvesHttpUtil.cancel(ElvesHttpConst.POST_OPEN_EXPEND)
        ElvesHttpUtil.cancel(ElvesHttpConst.POST_OPEN_INCOME)
        super.onDestroy()
    }



}
